package com.stockz.equity.manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.stockz.equity.dao.ScripDao;
import com.stockz.equity.model.Scrip;

@Component
public class ScripManagerImpl implements ScripManager{

	@Autowired private ScripDao scripDao;
	
	private List<Scrip> allScripList = new ArrayList<Scrip>();
	private List<String> allScripNameList = new ArrayList<String>();
	
	@Override
	public List<Scrip> getAllScripList(Date date) {
		return scripDao.getAllScripList(date);
	}
	
	@Override
	public List<String> getAllScripNameList(Date date) {
		List<Scrip> scripList = getAllScripList();
		if(scripList != null && !scripList.isEmpty()){
			List<String> scripNameList = new ArrayList<String>(scripList.size());
			for(Scrip scrip : scripList){
				if(StringUtils.hasText(scrip.getScripName())){
					scripNameList.add(scrip.getScripName());
				}
			}
			return scripNameList;
		}
		return Collections.emptyList();
	}
	
	@Override
	public List<Scrip> getAllScripList(){
		if(allScripList == null || allScripList.isEmpty()){
			allScripList = scripDao.getAllScripList();
			List<Scrip> removableScripList = new ArrayList<Scrip>();
			for(Scrip scrip : allScripList){
				if(!StringUtils.hasText(scrip.getScripName()) 
						|| !StringUtils.hasText(scrip.getScripCode())){
					removableScripList.add(scrip);
				}
			}
			allScripList.removeAll(removableScripList);
		}
		return allScripList;
	}
	
	@Override
	public List<String> getAllScripNameList() {
		if(allScripNameList == null ||
				allScripNameList.isEmpty()){
			List<Scrip> scripList = getAllScripList();
			if(scripList != null && !scripList.isEmpty()){
				allScripNameList = new ArrayList<String>(scripList.size());
				for(Scrip scrip : scripList){
					allScripNameList.add(scrip.getScripName());
				}
			}
		}
		return allScripNameList;
	}
	
	
	@Override
	public Scrip getScripByScripCode(String scripCode){
		for(Scrip scrip : allScripList){
			if(scrip.getScripCode().equalsIgnoreCase(scripCode)){
				return scrip;
			}
		}
		throw new IllegalArgumentException();
	}

	@Override
	public Scrip getScripByScripName(String scripName) {
		for(Scrip scrip : allScripList){
			if(scrip.getScripName() != null){
				if(scrip.getScripName().equalsIgnoreCase(scripName)){
					return scrip;
				}
			}
		}
		throw new IllegalArgumentException();
	}
	
	 @Override
	public void persist(Scrip scrip) {
		 if(scrip != null && !allScripList.contains(scrip)){
			 scripDao.persist(scrip);
			 allScripList.add(scrip);
			 if(StringUtils.hasText(scrip.getScripName())){
				 allScripNameList.add(scrip.getScripName());
			 }
		 }
	}
	 
	 public Scrip merge(Scrip scrip){
		 Scrip managed = scrip;
		 if(scrip != null && !allScripList.contains(scrip)){
			 managed = scripDao.merge(scrip);
			 allScripList.add(scrip);
			 if(StringUtils.hasText(scrip.getScripName())){
				 allScripNameList.add(scrip.getScripName());
			 }
		 }
		 return managed;
	 }
	 
}
